package cn.edu.wynu.api.stringapi;

public class StringBuilderLearn {
    public static void main(String[] args) {
        testStringBuilderAdd();
        testStringAdd();
    }

    private static void testStringBuilderAdd() {
        StringBuilder sb = new StringBuilder();
        long start = System.currentTimeMillis();
        for (int i = 0; i < 1000; i++) {
            sb.append(",").append(i);
        }
        long end = System.currentTimeMillis();
        System.out.println("StringBuilder拼接耗时：" + (end - start) + "ms");
        System.out.println(sb);
    }

    /**
     * 测试String自带的字符串拼接性能
     */
    private static void testStringAdd() {
        String s = "";
        // 开始时间
        long start = System.currentTimeMillis();
        for (int i = 0; i < 1000; i++) {
            s = s + "," + i;
        }
        // 结束时间
        long end = System.currentTimeMillis();
        System.out.println("String拼接耗时：" + (end - start) + "ms");
        System.out.println(s);
    }
}
